/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cliente.auxiliar;

import cliente.gui.Erro;
import cliente.responsehandler.BadGatewayExeption;
import cliente.responsehandler.BadRequest;
import cliente.responsehandler.ForbiddenExeption;
import cliente.responsehandler.InternalServerExeption;
import cliente.responsehandler.NotAcceptableExeption;
import cliente.responsehandler.NotFoundExeption;
import cliente.responsehandler.NotModifiedExeption;
import cliente.responsehandler.UnauthorizedExeption;
import cliente.responsehandler.UnavailableExeption;
import org.apache.http.HttpResponse;

/**
 *
 * @author ziza
 */
public class GereErros {

    GereOperacoes gestor = null;

    public GereErros(GereOperacoes gestor) {
        this.gestor = gestor;
    }

    public void trataErros(HttpResponse httpResponse) throws NotModifiedExeption, UnauthorizedExeption, ForbiddenExeption, NotFoundExeption, NotAcceptableExeption, InternalServerExeption, BadGatewayExeption, UnavailableExeption, BadRequest{

        String causa = "";
        causa = httpResponse.getStatusLine().getReasonPhrase();
        int codigo = httpResponse.getStatusLine().getStatusCode();
        new Erro(causa);
        System.out.println("Causa:"+causa+"\nCodigo: "+codigo);

        if(codigo == 304){
            throw new NotModifiedExeption(causa);
        }

        if(httpResponse.getStatusLine().getStatusCode() == 400){
            throw new BadRequest(causa);
        }

        if(codigo == 401){
            throw new UnauthorizedExeption(causa);
        }

        if(codigo == 403){
            throw new ForbiddenExeption(causa);
        }

        if(codigo == 404){
            throw new NotFoundExeption(causa);
        }

        if(codigo == 406){
            throw new NotAcceptableExeption(causa);
        }

        if(codigo == 500){
            gestor.getArquivador().setErro(true);
            throw new InternalServerExeption(causa);
        }

        if(codigo == 502){
            gestor.getArquivador().setErro(true);
            throw new BadGatewayExeption(causa);
        }

        if(codigo == 503){
            throw new UnavailableExeption(causa);
        }
    }

}
